package com.architecture.core.web.actions;

import com.architecture.core.except.ErrException;
import com.architecture.core.service.BaseService;
import com.architecture.core.web.vo.GroupVO;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * User: aguang
 * Date: 13-2-3
 * Time: 下午2:33
 * To change this template use File | Settings | File Templates.
 */
@Results({
        @Result(name = "list", location = "back/group/group_list.jsp"),

        @Result(name = "create_success", location = "back/group/group_list.jsp"),
        @Result(name = "update_success", location = "back/group/group_list.jsp"),
        @Result(name = "create_senior_success", location = "back/group/group_list.jsp"),
        @Result(name = "update_senior_success", location = "back/group/group_list.jsp"),

        @Result(name = "create_error", location = "back/group/group_create.jsp"),
        @Result(name = "update_error", location = "back/group/group_update.jsp"),
        @Result(name = "create_senior_error", location = "back/group/group_create.jsp"),
        @Result(name = "update_senior_error", location = "back/group/group_update.jsp"),

        @Result(name = "to_group_list", location = "back/group/group_list.jsp"),
        @Result(name = "to_group_create", location = "back/group/group_create.jsp"),
        @Result(name = "to_group_update", location = "back/group/group_update_senior.jsp"),

        @Result(name = "to_group_list_senior", location = "back/group/group_list_senior.jsp"),
        @Result(name = "to_group_create_senior", location = "back/group/group_create_senior.jsp"),
        @Result(name = "to_group_update_senior", location = "back/group/group_update_senior.jsp")

})
public class GroupAction extends AbstractAction {

    private GroupVO groupVO = new GroupVO();

    @Resource
    private BaseService baseService;

    @Override
    @Action(value = "group")
    public String execute() throws Exception {
        return LIST;    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Action(value = "create_group")
    public String create() throws ErrException {
        try {
            this.baseService.create(groupVO.getGroup());
            return CREATE_SUCCESS;
        } catch (ErrException err) {
            err.printStackTrace();
            super.addActionError(err.getMessage());
        }
        return CREATE_ERROR;
    }

    @Action(value = "update_group")
    public String update() {
        try {
            this.baseService.update(groupVO.getGroup());
            return UPDATE_SUCCESS;
        } catch (ErrException err) {
            err.printStackTrace();
            super.addActionError(err.getMessage());
        }
        return UPDATE_ERROR;
    }

    @Action(value = "to_group_create")
    public String toCreateGroup(){
        return "to_group_create";
    }

    @Action(value = "to_group_create_senior")
    public String toCreateGroupSenior(){
        return "to_group_create_senior";
    }

    @Action(value = "to_group_update")
    public String toUpdateGroup(){
        return "to_group_update";
    }

    @Action(value = "to_group_update_senior")
    public String toUpdateGroupSenior(){
        return "to_group_update_senior";
    }

    @Action(value = "to_group_list")
    public String toListGroup(){
        return "to_group_list";
    }

    @Action(value = "to_group_list_senior")
    public String toListGroupSenior(){
        return "to_group_list_senior";
    }

    public GroupVO getGroupVO() {
        return groupVO;
    }

    public void setGroupVO(GroupVO groupVO) {
        this.groupVO = groupVO;
    }
}
